package com.ruoyi.kjy.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.utils.SecurityUtils;
import com.ruoyi.kjy.domain.CreateSpace;
import com.ruoyi.kjy.domain.MigrationEnterprise;
import com.ruoyi.kjy.domain.ServiceAgency;
import com.ruoyi.kjy.service.MigrationEnterpriseService;
import com.ruoyi.kjy.service.ServiceAgencyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.Objects;

/**
 * @Description:
 * @Author: Hi，Wang
 * @Date: 12/31,0031 17:46
 */


@RestController
@RequestMapping("/migrationenterprise")
public class MigrationEnterpriseController extends BaseController {





    @Autowired
    private MigrationEnterpriseService migrationEnterpriseService;

    @GetMapping("/list")
    public AjaxResult list(Long current, Long size, MigrationEnterprise migrationEnterprise) {
        QueryWrapper<MigrationEnterprise> queryWrapper = new QueryWrapper<>();
        queryWrapper.setEntity(migrationEnterprise);
        queryWrapper.lambda().orderByDesc(MigrationEnterprise::getUpdateTime);

        if (Objects.isNull(current)) {
            current = 1L;
        }
        if (Objects.isNull(size)) {
            size = 10L;
        }
        if (size >= 20L) {
            size = 20L;
        }
        return AjaxResult.success(migrationEnterpriseService.page(new Page<>(current, size), queryWrapper));
    }


    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return AjaxResult.success(migrationEnterpriseService.getById(id));
    }

    @GetMapping(value = "my")
    public AjaxResult getInfo() {
        LambdaQueryWrapper<MigrationEnterprise> eq = new QueryWrapper<MigrationEnterprise>()
                .lambda().eq(MigrationEnterprise::getUserId, SecurityUtils.getUserId());
        eq.orderByDesc(MigrationEnterprise::getUpdateTime);
        return AjaxResult.success(migrationEnterpriseService.list(eq));
    }


    @Log(title = "迁入企业", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody MigrationEnterprise migrationEnterprise) {
//        System.out.println(migrationEnterprise);
        migrationEnterprise.setStatus(0);
        migrationEnterprise.setUserId(SecurityUtils.getUserId());
        boolean save = migrationEnterpriseService.save(migrationEnterprise);
        return toAjax(save);
    }

    @Log(title = "迁入企业", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody MigrationEnterprise migrationEnterprise) {
        return toAjax(migrationEnterpriseService.updateById(migrationEnterprise));
    }

    @Log(title = "迁入企业", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(migrationEnterpriseService.removeByIds(Arrays.asList(ids)));
    }

}
